package Controller;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import Jdbc.Jdbc;
import Jdbc.JdbcGokhan;
import Model.Dergi;
import Model.Makale;
import Model.Uye;

import com.opensymphony.xwork2.ActionSupport;

public class ControllerGokhan extends ActionSupport{
	
	private String konu;
	private String bilim_dali;
	private String dergi;
	private String etiket;
	private String baslik;
	private String editor;
	private String duzeltilecek_makale_adi;
	private InputStream inputStream;
	
	public String makaleYukleFormu() throws UnsupportedEncodingException{
		ArrayList<String> temp = new ArrayList<String>();
		String data = "s";
		Dergi dergi = new Dergi(null, null, null, null, null, null);
		temp = dergi.dergiIsimBul();
		for (int i = 0; i < temp.size(); i++){
				data = data + "," + temp.get(i);
		}
		data = data + "-";
		temp = JdbcGokhan.konu_bul();
		for (int i = 0; i < temp.size(); i++){
			data = data + "," + temp.get(i);
		}
		data = data + "-";
		temp = JdbcGokhan.bilim_dali_bul();
		for (int i = 0; i < temp.size(); i++){
			data = data + "," + temp.get(i);
		}
		inputStream = new ByteArrayInputStream(data.getBytes("UTF-8"));
		return SUCCESS;
	}
	
	public String makaleAsama1() throws UnsupportedEncodingException{
		setSession("makaleEkle_dergi", dergi);
		setSession("makaleEkle_konu", konu);
		setSession("makaleEkle_bilim_dali", bilim_dali);
		setSession("makaleEkle_etiket", etiket);
		setSession("makaleEkle_baslik", baslik);
		inputStream = new ByteArrayInputStream("success".getBytes("UTF-8"));
		return SUCCESS;
	}

	public String makaleAsama2() throws UnsupportedEncodingException {
		String editor;
		dergi = (String) getSession("makaleEkle_dergi");
		StringTokenizer token = new StringTokenizer(dergi, "*");
		editor = JdbcGokhan.derginin_editor_bul(token.nextToken());
		inputStream = new ByteArrayInputStream(editor.getBytes("UTF-8"));
		return SUCCESS;
	}
	
	public String makaleAsama3() throws UnsupportedEncodingException{
		setSession("makaleEkle_editor", editor);
		inputStream = new ByteArrayInputStream("success".getBytes("UTF-8"));
		return SUCCESS;
	}
	
	public String MakaleKaydet(String makale_yolu){
		ArrayList<String> konu = new ArrayList<String>();
		ArrayList<String> bilim_dali = new ArrayList<String>();
		String[] temp_konu = ((String) getSession("makaleEkle_konu")).split(",");
		for (int i = 0; i < temp_konu.length; i++){
			konu.add(temp_konu[i].trim());
		}
		
		String[] temp_bilim_dali = ((String)getSession("makaleEkle_bilim_dali")).split(",");
		for (int i = 0; i < temp_bilim_dali.length; i++){
			bilim_dali.add(temp_bilim_dali[i].trim());
		}
		Makale makale = new Makale(null, null, null, null, null, null, null, null, 0, 0, 0, 0, 0, null, null, null, null);
		return makale.makaleKaydet(Integer.parseInt(((Uye) getSession("uye")).getUye_id()), konu, bilim_dali, (String)getSession("makaleEkle_dergi"), (String)getSession("makaleEkle_etiket"), (String)getSession("makaleEkle_baslik"), (String)getSession("makaleEkle_editor"), makale_yolu);
	}
	
	public String makaleListeleIncelemede() throws UnsupportedEncodingException{
		Makale makale = new Makale(null, null, null, null, null, null, null, null, 0, 0, 0, 0, 0, null, null, null, null);
		String s = makale.makaleListeleYazar(Integer.parseInt(((Uye) getSession("uye")).getUye_id()), 0);
		inputStream = new ByteArrayInputStream(s.getBytes("UTF-8"));
		return SUCCESS;
	}
	
	public String makaleListeleDuzeltme() throws UnsupportedEncodingException{
		Makale makale = new Makale(null, null, null, null, null, null, null, null, 0, 0, 0, 0, 0, null, null, null, null);
		String s = makale.makaleListeleYazar(Integer.parseInt(((Uye) getSession("uye")).getUye_id()), 3);
		inputStream = new ByteArrayInputStream(s.getBytes("UTF-8"));
		return SUCCESS;
	}
	
	public String makaleListeleKabulEdilen() throws UnsupportedEncodingException{
		Makale makale = new Makale(null, null, null, null, null, null, null, null, 0, 0, 0, 0, 0, null, null, null, null);
		String s = makale.makaleListeleYazar(Integer.parseInt(((Uye) getSession("uye")).getUye_id()), 4);
		inputStream = new ByteArrayInputStream(s.getBytes("UTF-8"));
		return SUCCESS;
	}
	
	public String makaleListeleReddedilen() throws UnsupportedEncodingException{
		Makale makale = new Makale(null, null, null, null, null, null, null, null, 0, 0, 0, 0, 0, null, null, null, null);
		String s = makale.makaleListeleYazar(Integer.parseInt(((Uye) getSession("uye")).getUye_id()), 5);
		inputStream = new ByteArrayInputStream(s.getBytes("UTF-8"));
		return SUCCESS;
	}

	public String makaleDuzeltAsama1() throws UnsupportedEncodingException{
		StringTokenizer token = new StringTokenizer(duzeltilecek_makale_adi, "*");
		int makale_pk = Integer.parseInt(token.nextToken());
		setSession("duzeltilecek_makale_pk", makale_pk);
		inputStream = new ByteArrayInputStream("".getBytes("UTF-8"));
		return SUCCESS;
	}
	
	public String MakaleDuzelt(String makale_yolu){
		Makale makale = new Makale(null, null, null, null, null, null, null, null, 0, 0, 0, 0, 0, null, null, null, null);
		int makale_pk = (int) getSession("duzeltilecek_makale_pk");
		return makale.makaleDuzelt(makale_pk, makale_yolu);
	}
	
	public String getKonu() {
		return konu;
	}

	public void setKonu(String konu) {
		this.konu = konu;
	}

	public String getBilim_dali() {
		return bilim_dali;
	}

	public void setBilim_dali(String bilim_dali) {
		this.bilim_dali = bilim_dali;
	}

	public String getDergi() {
		return dergi;
	}

	public void setDergi(String dergi) {
		this.dergi = dergi;
	}

	public String getEtiket() {
		return etiket;
	}

	public void setEtiket(String etiket) {
		this.etiket = etiket;
	}

	public String getBaslik() {
		return baslik;
	}

	public void setBaslik(String baslik) {
		this.baslik = baslik;
	}
	
	public String getEditor() {
		return editor;
	}

	public void setEditor(String editor) {
		this.editor = editor;
	}
	
	
	
	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	private static void setSession(String key, Object obj) {
		HttpSession session = ServletActionContext.getRequest().getSession();
		session.setAttribute(key, obj);
	}
	
	private static Object getSession(String key) 
	{
	HttpSession session = ServletActionContext.getRequest().getSession();
	return session.getAttribute(key);
	}

	public String getDuzeltilecek_makale_adi() {
		return duzeltilecek_makale_adi;
	}

	public void setDuzeltilecek_makale_adi(String duzeltilecek_makale_adi) {
		this.duzeltilecek_makale_adi = duzeltilecek_makale_adi;
	}

}
